﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using S.Common.Enums.BreedEnum;
using S.Common.Enums;
using S.Common.Enums.Archives;
using Newtonsoft.Json;
using S.Common.helper;
using S.Authorization.Common.Dto;
using S.Breed.Domain.Dto;
using Microsoft.IdentityModel.Tokens;
using static S.Breed.Read.ApI.Application.Command.MaternalMatingCommand;
using MediatR;
using Org.BouncyCastle.Asn1.Ocsp;
using Microsoft.AspNetCore.Mvc.RazorPages;
using S.Common.ApiResult;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using S.Breed.Read.ApI.Application.Command;
using Microsoft.AspNetCore.Authorization;
using Authorization.Center.Controllers;
using Authorization.Center.SqlServer;

namespace S.Breed.Read.ApI.Controllers
{
    /// <summary>
    /// 种母配种读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ReadMaternalMatingController : BaseController
    {
        private readonly IMediator mediator;
        private readonly IConfiguration configuration;
        private readonly IHttpContextAccessor httpContextAccessor;

        public ReadMaternalMatingController(IMediator _mediator, MySqlDbContext db,IConfiguration configuration, IHttpContextAccessor httpContextAccessor) : base(db)
        {
            mediator = _mediator;
            this.configuration = configuration;
            this.httpContextAccessor = httpContextAccessor;
        }
        /// <summary>
        /// 种母发情类型
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult EstrusEnumShow()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<EstrusEnum>())
            {
                list.Add(new
                {
                    value = item,
                    name = item.ToString(),
                });
            }
            return Ok(list);

        }
        /// <summary>
        /// 转入栋舍
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult TransferBuildingEnumShow()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<TransferBuildingEnum>())
            {
                list.Add(new
                {
                    value = item,
                    name = item.ToString(),
                });
            }
            return Ok(list);

        }
        /// <summary>
        /// 配种方式
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult BreedingShow()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<BreedingTypeEnum>())
            {
                list.Add(new
                {
                    value = item,
                    name = item.ToString(),
                });
            }
            return Ok(list);

        }
        /// <summary>
        /// 繁殖状态
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult ReproductiveStateShow()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<Common.Enums.BreedEnum.ReproductiveStateEnum>())
            {
                list.Add(new
                {
                    value = item,
                    name = item.ToString(),
                });
            }
            return Ok(list);

        }
        /// <summary>
        /// 品种
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult VarietyEnumShow()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<VarietyEnum>())
            {
                list.Add(new
                {
                    value = item,
                    label = item.ToString(),
                });
            }
            return Ok(list);

        }
        /// <summary>
        /// 配种信息显示
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<ApiResult<List<ShowMaternalMatingDto>>> GetMaternal(string? mdate,string? ear,int head,int page,int size)
        {
            var list = await mediator.Send(new ShowMaternalMatingCommand(mdate, ear, head, page, size));
            list.StatusCode = 200;
            list.Message = "success";
            list.Timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");

            return list;
        }
        /// <summary>
        /// 栋舍
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetFieldModel()
        {
            string Gateway = configuration.GetValue<string>("Gateway");
            var jsonString = HttpClientHelper.GetResponse(Gateway+ "/Authorization/api/ColumnManagement/GetColumnList");
            var response = JsonConvert.DeserializeObject<List<SelectDto>>(jsonString);
            return Ok(response);
        }
        /// <summary>
        /// 栏位
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetHouseField(int id)
        {
            string Gateway = configuration.GetValue<string>("Gateway");
            var jsonString = HttpClientHelper.GetResponse(Gateway+$"/Authorization/api/ColumnManagement/GetHouseField?id={id}");
            var response = JsonConvert.DeserializeObject<List<SelectDto>>(jsonString);
            return Ok(response);
        }
        /// <summary>
        /// 跨服务请求负责人信息
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetUser()
        {
            string Gateway = configuration.GetValue<string>("Gateway");
            var jsonString = HttpClientHelper.GetResponse(Gateway+$"/Authorization/api/UserRead/GetUser");
            // 反序列化并显示栋舍数据
            var response = JsonConvert.DeserializeObject<List<SelectDto>>(jsonString);
            return Ok(response);
        }
        /// <summary>
        /// 获取种公信息
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetSelMaleEmalemale()
        {
            string Gateway = configuration.GetValue<string>("Gateway");
            var jsonString = HttpClientHelper.GetResponse(Gateway + "/ach/api/Provide/SelMaleEmale?id=" + 1);

            var response = JsonConvert.DeserializeObject<List<SelMaleEmaleDto>>(jsonString);

            return Ok(response);
        }
        /// <summary>
        /// 批量获取种母信息显示查询
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult PostSelMaleEmalefemale(string? pid, string? state, int MinMonth, int MaxMonth, string? build, string? colim)
        {
            string Gateway = configuration.GetValue<string>("Gateway");
            var jsonString = HttpClientHelper.GetResponse(Gateway + "/ach/api/Provide/SelMaleEmale?id=" + 2);

            var response = JsonConvert.DeserializeObject<List<SelMaleEmaleDto>>(jsonString);

            response = response.Where(t =>
                (string.IsNullOrEmpty(pid) || pid.Contains(t.Variety.ToString())) &&
                (string.IsNullOrEmpty(state) || (state == "0" || state.Contains(t.ReproductiveState.ToString()))) &&
                (MinMonth == 0 || t.MonthAge >= MinMonth) &&
                (MaxMonth == 0 || t.MonthAge < MaxMonth) &&
                (string.IsNullOrEmpty(build) || t.BuildingName == build) &&
                (string.IsNullOrEmpty(colim) || t.ColumnName == colim)).ToList();

            return Ok(response);
        }
        /// <summary>
        /// 获取种母信息下拉
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetSelMaleEmalefemale()
        {
            string Gateway = configuration.GetValue<string>("Gateway");
            var jsonString = HttpClientHelper.GetResponse(Gateway + "/ach/api/Provide/SelMaleEmale?id=" + 2);

            var response = JsonConvert.DeserializeObject<List<SelMaleEmaleDto>>(jsonString); 
            return Ok(response);
        }

        /// <summary>
        /// 通过种母耳号查档案信息
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetEarInfo(string Ear)
        {
            string Gateway = configuration.GetValue<string>("Gateway");
            var jsonString = HttpClientHelper.GetResponse(Gateway + "/ach/api/Provide/SelMaleEmale?id=" + 2);

            var response = JsonConvert.DeserializeObject<List<SelMaleEmaleDto>>(jsonString);

           var data= response.Where(t=>t.EarTag == Ear).ToList();
            return Ok(data);
        }

        /// <summary>
        /// 获取配种后的耳号
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetBreedEartag()
        {
            return Ok(await mediator.Send(new BreedEartagListCommand()));
        }
        /// <summary>
        /// 通过种母耳号查信息
        /// </summary>
        /// <param name="ear"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> ShowRamEar(string ear)
        {
            return Ok(await mediator.Send(new BreedShowRamEarCommand(ear)));
        }
        /// <summary>
        /// 反填配种种母信息
        /// </summary>
        /// <param name="ear"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> ShowRamEars(int id)
        {
            return Ok(await mediator.Send(new FanTinaCommand(id)));
        }
    }  
}
